/* File:        	RequestConturImpl.java
* Package:			org.esi.authz.pdp.request.contur
* Created:      	29-abr-2011
* Last Changed: 	0:10:46
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.common.request.contur;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Logger;

import org.jboss.security.xacml.core.model.context.RequestType;
import org.jboss.security.xacml.factories.RequestResponseContextFactory;
import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.XACMLConstants;
import org.jboss.security.xacml.sunxacml.attr.AttributeValue;
import org.jboss.security.xacml.sunxacml.ctx.Attribute;
import org.jboss.security.xacml.sunxacml.ctx.Subject;
import org.jboss.security.xacml.sunxacml.ctx.RequestCtx;

import com.tecnalia.authz.common.log.PDPLog;
import com.tecnalia.authz.common.request.component.ActionAttrTypeImpl;
import com.tecnalia.authz.common.request.component.IRequestAuthorization;
import com.tecnalia.authz.common.request.component.RequestAuthorizationImpl;
import com.tecnalia.authz.common.request.component.ResourceAttrTypeImpl;
import com.tecnalia.authz.common.request.component.SubjectAttrTypeImpl;
import com.tecnalia.authz.common.request.xacml.XACMLRequestImpl;

// TODO: Auto-generated Javadoc
/**
 * The Class RequestConturImpl.
 */
public class RequestConturImpl extends XACMLRequestImpl implements IRequestContur{
	
	private RequestContext requestContext = null;
	
	@Override
	public RequestContext getRequestContext() {
		return requestContext;
	}

	@Override
	public void setRequestContext(RequestContext requestContext) {
		this.requestContext = requestContext;
	}


	public RequestContext[] flexibleRequestContext() throws Exception{
		if(requestContext == null){
			Exception e = new Exception("Request to convert is null");
			e.fillInStackTrace();
			throw e;
		}
		

		
		RequestCtx req = this.requestContext.get("request_ctx");
		
		String[] subjectSplit = new String[1];
		
		Set<Subject> subjects = req.getSubjects();
		for (Iterator<Subject> iterator = subjects.iterator(); iterator.hasNext();) {
			Subject subject = iterator.next();
			for (Iterator<Attribute> iterator2 = subject.getAttributes().iterator(); iterator2.hasNext();) {
				Attribute attr = iterator2.next();
				AttributeValue subjectS = attr.getValue();
				String subjectString = subjectS.encode();
				subjectSplit = subjectString.split(",");				
			}					
		}			
		
		String[] serviceSplit = null;			
		Set resources = req.getResource();
		for (Iterator<Attribute> iterator = resources.iterator(); iterator.hasNext();) {
			Attribute attr = iterator.next();
			AttributeValue service = attr.getValue();
			String serviceString = service.encode();
			serviceSplit = serviceString.split(",");			
		}
		
		String[] actionSplit = null;			
		Set actions = req.getAction();
		for (Iterator<Attribute> iterator = actions.iterator(); iterator.hasNext();) {
			Attribute attr = iterator.next();
			AttributeValue action = attr.getValue();
			String actionString = action.encode();
			actionSplit = actionString.split(",");			
		}
		
		return CreateListRequest(subjectSplit[0],
								serviceSplit,
								actionSplit[0],
								"non-functional-group");
		
    }
    
	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.request.contur.IRequestContur#parseRequestContext2(org.jboss.security.xacml.interfaces.RequestContext)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public RequestContext strictRequestContext() throws Exception{
		
		RequestCtx req = this.requestContext.get("request_ctx");
		
		String[] subjectSplit = new String[1];
		
		Set subjects = req.getSubjects();
		if(subjects.size() > 2){
			Exception e = new Exception("No more than one subject allowed.");
			e.fillInStackTrace();
			throw e;
		}

		for (Iterator<Subject> iterator = subjects.iterator(); iterator.hasNext();) {
			Subject subject = iterator.next();
			for (Iterator<Attribute> iterator2 = subject.getAttributes().iterator(); iterator2.hasNext();) {
				Attribute attr = iterator2.next();
				AttributeValue subjectS = attr.getValue();
				String subjectString = subjectS.encode();
				subjectSplit = subjectString.split(",");				
			}					
		}			
		
		String[] serviceSplit = null;			
		Set resources = req.getResource();
		for (Iterator<Attribute> iterator = resources.iterator(); iterator.hasNext();) {
			Attribute attr = iterator.next();
			AttributeValue service = attr.getValue();
			String serviceString = service.encode();
			serviceSplit = serviceString.split(",");			
		}
		
		String[] actionSplit = null;			
		Set actions = req.getAction();
		if(actions.size() > 2){
			Exception e = new Exception("No more than one action allowed.");
			e.fillInStackTrace();
			throw e;
		}
		for (Iterator<Attribute> iterator = actions.iterator(); iterator.hasNext();) {
			Attribute attr = iterator.next();
			AttributeValue action = attr.getValue();
			String actionString = action.encode();
			actionSplit = actionString.split(",");			
		}
		
		return CreateRequest(subjectSplit[0],
											serviceSplit,
											actionSplit[0],
											"non-functional-group");
		
    }	

    
    
}
